<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="学院ID" prop="collegeId">
        <el-input v-model="form.collegeId" placeholder="请输入学院ID" />
      </el-form-item>
      <el-form-item label="上课时间ID" prop="studyTimeId">
        <el-input v-model="form.studyTimeId" placeholder="请输入上课时间ID" />
      </el-form-item>
      <el-form-item label="星期" prop="weekId">
        <el-input v-model="form.weekId" placeholder="请输入星期" />
      </el-form-item>
      <el-form-item label="班级ID" prop="classesId">
        <el-input v-model="form.classesId" placeholder="请输入班级ID" />
      </el-form-item>
      <el-form-item label="课程ID" prop="subjectId">
        <el-input v-model="form.subjectId" placeholder="请输入课程ID" />
      </el-form-item>
      <el-form-item label="教室信息" prop="roomId">
        <el-input v-model="form.roomId" placeholder="请输入教室信息" />
      </el-form-item>
      <el-form-item label="教师信息" prop="teacherId">
        <el-input v-model="form.teacherId" placeholder="请输入教师信息" />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.STUDY_TIMETABLE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上课模式 " prop="classMode">
        <el-select v-model="form.classMode" placeholder="请选择上课模式 ">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.STUDY_TIMETABLE_MODE)"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createClassTimetable, updateClassTimetable } from "@/api/study/classTimetable";

export default {
  name: "ClassTimetableEdit",
  components: {
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studyTimeId: [{ required: true, message: "上课时间ID不能为空", trigger: "blur" }],
        weekId: [{ required: true, message: "星期不能为空", trigger: "blur" }],
        classesId: [{ required: true, message: "班级ID不能为空", trigger: "blur" }],
        subjectId: [{ required: true, message: "课程ID不能为空", trigger: "blur" }],
        roomId: [{ required: true, message: "教室信息不能为空", trigger: "blur" }],
        teacherId: [{ required: true, message: "教师信息不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        classMode: [{ required: true, message: "上课模式 不能为空", trigger: "change" }],
      }
    };
  },
  created() { },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: ''
    }
  },
  methods: {
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime]
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit('update:show', false)
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        collegeId: undefined,
        studyTimeId: undefined,
        weekId: undefined,
        classesId: undefined,
        subjectId: undefined,
        roomId: undefined,
        teacherId: undefined,
        type: undefined,
        classMode: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateClassTimetable(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit('success')
          });
          return;
        }
        // 添加的提交
        createClassTimetable(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit('success')
        });
      });
    },

  }
};
</script>
